package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.FbatBinaryForm;
import ca.ubc.icapture.genapha.forms.AnalysisFilesForm;

import icapture.SQLMgr;
import icapture.genapha.GenotypeToolsManager;
import icapture.beans.DB.User;
import icapture.beans.DBExportOptions;
import icapture.beans.Format;

import icapture.genapha.exporters.*;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:38:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class FbatBinaryAction extends AnalysisFilesAction
{

    @Override
    protected void processRequest(AnalysisFilesForm form)
    {
        final FbatBinaryForm binaryForm = (FbatBinaryForm) form;
        final DBExporter exporter;
        DBExportOptions options = new DBExportOptions();
        GenotypeToolsManager gtm = new GenotypeToolsManager();
        final Format format;

        // Add name to the result
        if (binaryForm.getSelectedCohort().length > 1)
        {
            result.setName("Combined Binary Phenotype Linkage File");
        }
        else
        {
            result.setName("Binary Phenotype Linkage File");
        }
        SQLMgr.updateResult(result);

        // Init exporter and format
        if (binaryForm.getFormat().equalsIgnoreCase("SAGE"))
        {
            exporter = new FbatDBExporter(options);
            format = gtm.SAGE_FORMAT;
        }
        else if (binaryForm.getFormat().equalsIgnoreCase("UNPHASED"))
        {
            exporter = new UnphasedDBExporter(options);
            format = gtm.UNPHASED_FORMAT;
            // GOTCHA: No need to worry about format's unaffected
            // because Busselton is not part of NHL
        }
        else
        {
            exporter = new FbatDBExporter(options);
            format = gtm.FBAT_FORMAT;
        }

        // Setup options
        options.setPhenotypeID(Integer.valueOf(binaryForm.getSelectedPhenotype()));
        options.setCohortIds(binaryForm.getSelectedCohort());
        options.setRemoveMZTwins(binaryForm.isRemoveMzTwins());
        options.setRemoveDups(binaryForm.isRemoveDuplicateSubjects());
        options.setGeneIDs(binaryForm.getSelectedGenes());
        options.setEthnicityIDs(null);
        options.setResult(result);
        options.setFormat(format);
        options.setFilePerGene(binaryForm.isFilePerGene());
        options.setCohortPopulationIDs(binaryForm.getSelectedCohortSubPopulation());
        options.setZeroIncomplete(binaryForm.isZeroIncomplete());
        options.setZeroControls(binaryForm.isZeroControls());
        User user = new User();
        user.setEmail(binaryForm.getEmail());
        options.setUser(user);

        exporter.export();
    }
}
